package Principal;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

public class TablaSimbolos {

	private ArrayList <Simbolos> tabla = new ArrayList <Simbolos>();
	private int BlockLevel = 0;
	
	public int getBlockLevel() {
		return BlockLevel;
	}

	public void setBlockLevel(int blockLevel) {
		BlockLevel = blockLevel;
	}

	public void blkPush() {		
		this.BlockLevel++;
	}
	
	public void blkPop() {		
		this.BlockLevel--;
	}
	
	public void create(String nomb,TipSimb tsimb,int tam, int dir) {
		Simbolos aux = new Simbolos(nomb, tsimb, this.BlockLevel, tam, dir);
			
		
		tabla.add(aux);
	    
		//tabla.put(nomb,aux)
		
		
	}
	
	public void create(String nomb,TipSimb tsimb,int tam, int dir,int npar) {
		Simbolos aux = new Simbolos(nomb, tsimb, this.BlockLevel, tam, dir, npar);
			
		
		tabla.add(aux);
	    
		//tabla.put(nomb,aux);
		
	}
	public void create(String nomb,int tam, int dir) {
		Simbolos aux = new Simbolos(nomb, TipSimb.UDEC, this.BlockLevel, tam, dir);
		
		tabla.add(aux);
		
		//tabla.put(nomb,aux);
		
	}
	// recuperar un historico de valor de una variable
	  public int find(String name)  {
		  Simbolos value = null;	
		  boolean enc = false;
		  
		  int i = tabla.size()-1;		  
		  while ((!enc)&&(i>=0)) {
			 value = tabla.get(i);
			 if (name.compareTo(value.getNombre()) == 0) 
					 enc = true;
			 else
				 i--;
		  }
		  if (enc) 
		     return i;
		  else		 	   		 
			return -1;	  
	  }
	
	 // registrar un valor en el historico de una variable
	  public Simbolos lookup(String name,int tam, int dir) {
	    Simbolos aux = null;
	    int i = -1;
	    
	    
	    i = find(name);
		if (i == -1) {			
			aux = new Simbolos(name, TipSimb.UDEC, 0, tam, dir);		
			System.out.print(aux);
			tabla.add(aux);
			
			//tabla.put(name,aux);
		}
		else
			aux = tabla.get(i);
	    return aux;
	    
	  }
	  
	  public String toString() {
		  String aux = "";
		  Iterator<Simbolos> itr = tabla.iterator();
		  Simbolos r;	
		  
		  while(itr.hasNext()) {
			  r = itr.next();  
			  aux = aux.concat( r.toString() + ", " );
			}
		  return aux;
	  }
	  
	
}
